<template>
  <view class="share-container">
    <uni-forms ref="shareForm" :model="formData" :rules="rules">
      <uni-forms-item label="用户" name="username" required>
        <uni-easyinput v-model="formData.username" placeholder="请输入用户名" />
      </uni-forms-item>
      <uni-forms-item label="邮箱" name="email" required>
        <uni-easyinput v-model="formData.email" placeholder="请输入邮箱"  />
      </uni-forms-item>
	  <uni-forms-item label="歌单" name="title" required>
	    <uni-easyinput v-model="formData.title" placeholder="请给收藏歌单起个名称"  />
	  </uni-forms-item>
	  <uni-forms-item label="封面" name="cover" >
	    <uni-easyinput v-model="formData.cover" placeholder="请输入歌单封面图(可选)https://"  />
	  </uni-forms-item>
      <uni-forms-item label="备注" name="note" >
        <uni-easyinput type="textarea" autoHeight v-model="formData.note" placeholder="请输入备注信息" />
      </uni-forms-item>
      <button type="primary" @click="submitForm">提交分享</button>
    </uni-forms>
  </view>
</template>

<script>
	import favorites from '@/utils/audiofavorites';
	import { createSongMenu } from '@/api/music.js';
export default {
  data() {
    return {
      formData: {
        username: '',
        email: '',
		title:'',
		cover:'',
		note:'',
		songs:[],
		myfavs:[]
      },
      rules: {
        username: {
          rules: [{ required: true, errorMessage: '用户名不能为空' }]
        },
        email: {
          rules: [{ required: true, errorMessage: '邮箱不能为空' }, { format: 'email', errorMessage: '请输入正确的邮箱地址' }]
        },
		title: {
		  rules: [{ required: true, errorMessage: '歌单名称不能为空' }]
		},
      }
    };
  },
  onLoad() {
  	console.log('musicshare onload');
  	favorites.initAudioFavorites();
  	this.myfavs = favorites.getAllAudioFavorites();
 
  	if(this.myfavs.length > 0){
  		this.myfavs.forEach(item => {
			let s = {};
			s.sid = item.id;
			s.cover = item.cover;
			s.sing = item.sing;
			s.song = item.song;
			s.url = item.url;
			this.formData.songs.push(s);
  		});
  	}
  },
  methods: {
    submitForm() {
      this.$refs.shareForm.validate().then(res => {
        console.log('表单数据:', res);
		if(this.myfavs.length <= 0){
			uni.showToast({
			  title: '提交失败,'+'歌单为空',
			   duration: 1500,
			  icon: 'none'
			});
			return;
		}
        createSongMenu(res.title,res.cover,res.username,res.email,res.note,
                       this.formData.songs).then(result => {
        				console.log("createSongMenu,result:");
        				console.log(result);
        				if(result.code == 0){
        					uni.showToast({
        					  title: '提交成功,感谢分享!',
							  duration: 1500,
        					  icon: 'none'
        					});
        					// 提交成功后返回上一页
							setTimeout(() => {
							   uni.navigateBack();
							}, 1000); // 延时1000毫秒
        					
        				}else{
        					uni.showToast({
        					  title: '提交失败,'+result.message,
							   duration: 1500,
        					  icon: 'error'
        					});
        				}
        				
        			});
      }).catch(err => {
        console.log('表单校验失败:', err);
      });
    }
  }
};
</script>

<style scoped>
.share-container {
  padding: 20rpx;
  flex-direction: column;

}
</style>